package com.house.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.house.mapper.UserMapper;
import com.house.entity.User;
import com.house.service.UserService;
import com.house.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(String username) {
        User u = userMapper.findByUsername(username);
        return u;
    }

    @Override
    public void register(String username, String password, String role) {
        //加密
        String md5String = Md5Util.getMD5String(password);
        //添加
        userMapper.add(username,md5String,role);
    }

    @Override
    public void update(User user) {
        user.setUpdated_at(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updatePwd(String newPwd) {
//        //从拦截器获取id
//        Map<String,Object> map = ThreadLocalUtil.get();
//        Integer id = (Integer) map.get("id");
        Integer id = StpUtil.getLoginIdAsInt(); // 获取当前登录用户的 ID

        //加密
        String  md5String = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(md5String,id);
    }

}
